package com.maoye.ossp.bpm.bpmsap.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.maoye.ossp.bpmagent.context.BpmAgentConfiguration;
import com.maoye.ossp.common.service.IDataBean;
import com.maoye.ossp.common.util.RequestContext;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.api.local.UserService;

@Service("optionalPersonnelService")
public class OptionalPersonnelServiceImpl implements IDataBean {
	// --------------------------属性-------------------------------
	@Resource(name = "bpmAgentConfiguration")
	private BpmAgentConfiguration bpmAgentConfiguration;

	@Resource
	private UserService<User> userService;

	@Override
	public List<Object> getDataList(RequestContext request) throws Exception {
		List<Object> mapList = Lists.newArrayList();

		String proDefId = request.getParameter("proDefId");
		String nodeDefId = request.getParameter("nodeDefId");
		Map<String,Object> map = Maps.newHashMap();
		map.put("id", "0");
		map.put("name", "人员备选列表");
		map.put("chkDisabled", true);
		mapList.add(map);
		try {
			String result = bpmAgentConfiguration.getBpmAgentContext()
					.getProcessInstanceEngine("processInstanceEngineImpl")
					.getUsersOptional(proDefId, nodeDefId);
			JSONObject resultJson = JSONObject.fromObject(result);
			if ("1002".equals(resultJson.getString("code"))) {
				throw new Exception(resultJson.getString("msg"));
			}
			String userIds = resultJson.getString("data");
			List<User> userList = userService.getUsersByIds(userIds
					.split(";"));
			for (User worker : userList) {
				Map<String,Object> varMap = Maps.newHashMap();
				varMap.put("id", worker.getId().toString());
				varMap.put("name", worker.getName());
				varMap.put("pId", "0");
				mapList.add(varMap);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return mapList;
	}

	// -------------------------get/set-------------------------
}
